package com.easy.work.function.me.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.base.syslib.util.MessageTools;
import com.easy.work.R;
import com.easy.work.function.resume.AddWorkExperienceAct;
import com.easy.work.http.BaseResponse;
import com.easy.work.http.HttpClient;
import com.easy.work.http.request.DeleteWorkExperienceRequest;
import com.easy.work.http.response.WorkExperienceResponse;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by admin on 2017/12/24.
 * 工作经验
 */
public class WorkExperienceAdapter extends BaseAdapter {
    private Context context;
    /**
     * 工作经历
     */
    private List<WorkExperienceResponse> showList;

    public WorkExperienceAdapter(Context context, List<WorkExperienceResponse> workExperienceResponseList) {
        this.context = context;
        this.showList = workExperienceResponseList;
    }

    @Override
    public int getCount() {
        return showList.size();
    }

    @Override
    public Object getItem(int position) {
        return showList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_work_experience, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final WorkExperienceResponse response = showList.get(position);
        viewHolder.tvWorkTime.setText(response.dateFrom + "-" + response.dateTo);
        viewHolder.tvZhiweiName.setText("职位：" + response.job);
        viewHolder.tvGongsiName.setText("公司：" + response.company);
        viewHolder.tvDetail.setText(response.description);
        viewHolder.imgDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                deleteExperience(response, position);

            }
        });
        viewHolder.imgEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //修改工作经历
                MessageTools.showDialog((Activity) context, "是否确定修改本工作经历？", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        AddWorkExperienceAct.startActivity(context, response);
                    }
                });

            }
        });

        return convertView;
    }

    /**
     * 删除工作经历
     *
     * @param response
     * @param position
     */
    private void deleteExperience(final WorkExperienceResponse response, final int position) {
        MessageTools.showDialog((Activity) context, "是否确定删除该工作经历？", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                //删除工作经历
                DeleteWorkExperienceRequest request = new DeleteWorkExperienceRequest();
                request.experienceId = response.experienceId;
                HttpClient.post(request, new HttpClient.onSuccessListener() {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        showList.remove(position);
                        notifyDataSetChanged();

                    }
                }, new HttpClient.onFailListener() {
                    @Override
                    public void onFail(int code, String msg) {
                        MessageTools.showToast(context, msg);
                    }
                });
            }
        });

    }

    static class ViewHolder {
        @BindView(R.id.tv_work_time)
        TextView tvWorkTime;
        @BindView(R.id.tv_zhiwei_name)
        TextView tvZhiweiName;
        @BindView(R.id.tv_gongsi_name)
        TextView tvGongsiName;
        @BindView(R.id.tv_detail)
        TextView tvDetail;
        @BindView(R.id.ll_mid)
        LinearLayout llMid;
        @BindView(R.id.img_edit)
        ImageView imgEdit;
        @BindView(R.id.img_delete)
        ImageView imgDelete;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
